﻿using ABlog.Domain.Entity;
using ABlog.Domain.FormModel;
using ABlog.Domain.Infrastructure;
using ABlog.Domain.Repository;

namespace ABlog.Domain.Service
{
    public class UserService : IUserService
    {
        private readonly IUserIndexRepository _userIndexRepository;
        public UserService(IUserIndexRepository userIndexRepository)
        {
            _userIndexRepository = userIndexRepository;
        }

        public bool AddUser(UserFormModel userFormModel)
        {
            if (_userIndexRepository.FindUserInfoByAccountName(userFormModel.Name) != null)
                throw new FriendlyException($"{userFormModel.Name}已经被注册", $"{userFormModel.Name}已经被注册");
            _userIndexRepository.AddModel(new UserIndexEntity(userFormModel.Name));
            return _userIndexRepository.SaveChange()>0;
        }
    }
}
